package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author Guoyh
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        shoppingCart.setCreateTime(LocalDateTime.now());
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        if (dishId != null) {
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getUserId,userId).eq(ShoppingCart::getDishId, dishId);
            ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
            if (shoppingCart1 != null) {
                Integer count = shoppingCart1.getNumber();
                shoppingCart1.setNumber( count + 1);
                shoppingCartService.updateById(shoppingCart1);
                return R.success("新增菜品成功");
            }
            shoppingCartService.save(shoppingCart);
            return R.success("新增菜品成功");
        } else if (setmealId != null){
            LambdaQueryWrapper<ShoppingCart> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ShoppingCart::getUserId,userId).eq(ShoppingCart::getSetmealId, setmealId);
            ShoppingCart shoppingCart2 = shoppingCartService.getOne(queryWrapper1);
            if (shoppingCart2 != null) {
                Integer count = shoppingCart2.getNumber();
                shoppingCart2.setNumber( count + 1);
                shoppingCartService.updateById(shoppingCart2);
                return R.success("新增套餐成功");
            }
            shoppingCartService.save(shoppingCart);
            return R.success("新套餐成功");
        }
        return R.error("新增失败");
    }


    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }


    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null, ShoppingCart::getUserId,userId);
        shoppingCartService.remove(queryWrapper);
        return R.success("已成功清空购物车!");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody Map map, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        String dishId = (String) map.get("dishId");
        String setmealId = (String) map.get("setmealId");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null, ShoppingCart::getUserId,userId);
        queryWrapper.eq(dishId!=null,ShoppingCart::getDishId,dishId);
        queryWrapper.eq(setmealId!=null,ShoppingCart::getSetmealId,setmealId);

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        Integer count = one.getNumber();
        if (count == 1) {
            shoppingCartService.remove(queryWrapper);
            return R.success("删减成功");
        } else {
            one.setNumber( count - 1);
            shoppingCartService.updateById(one);
            return R.success("删减成功");
        }
    }
}

